<template>
	<view>
		<div>
			<navitop>
				<i class="iconfont" slot="left-slot" @click="outlot">&#xe667;</i>
				<span slot="center-slot">{{titles}}</span>
				<!-- <i class="iconfont " slot="right-slot" @click="add"></i> -->
			</navitop>
		</div>
		<div :style="'padding-top:' + nv_height">
			<div class="bill_sty" v-if="deliver_tpye == 0">
				<div class="bill_list" v-for="(item,index) in dataSorce"
					v-if="item.dataTab == '表头' && item.code == 'onEdit' || item.code == 'purchaseTypeId' || item.code == 'vendorId'  || item.code == 'crmId' || (item.code == 'warehouseId' && item.dataTab == '表头')">
					<div class="bill_title">
						{{item.name}}:
					</div>
					<div class="bill_input" @click="choice(item)">
						<input v-model="item.values" disabled />
					</div>
				</div>
			</div>
			<div class="bill_content" v-if="deliver_tpye == 0">
				<div class="bill_content_title">
					物料选择
				</div>
				<div class="bill_manual">

					<div class="bill_manual_choose">
						<span @click="sweep_code">扫码获取</span>
						<span @click="material">选择物料</span>
					</div>

				</div>
			</div>
			<div class="bill_content" v-for="(item,index) in Code_dataSorce" v-if="deliver_tpye == 0"
				@click="content_click(item)">
				<div class="bill_content_list">
					<div class="bill_title">物料编码:</div>
					<div class="bill_input_rst">
						{{item.inventoryCode}}
					</div>
				</div>
				<div class="bill_content_list">
					<div class="bill_title">物料名称:</div>
					<div class="bill_input_rst">
						{{item.inventoryName}}
					</div>
				</div>
				<div class="bill_content_list">
					<div class="bill_title">规格型号:</div>
					<div class="bill_input_rst">
						{{item.inventorySpecification}}
					</div>
				</div>

				<div class="bill_content_list">
					<div class="bill_title">{{text_list}}:</div>
					<div class="bill_input_rst">
						{{item.baseQuantity}}
					</div>
				</div>
			</div>

			<div v-if="deliver_tpye == 1" class="bill_content">
				<div class="bill_content_list">
					<div class="bill_title">单号：</div>
					<div class="bill_input_rst">
						{{Code_dataSorce.voucherCode}}
					</div>
				</div>
				<div class="bill_content_list">
					<div class="bill_title">时间：</div>
					<div class="bill_input_rst">
						{{Code_dataSorce.voucherDate}}
					</div>
				</div>
				<div class="bill_content_list">
					<div class="bill_title">创建人：</div>
					<div class="bill_input_rst">
						{{Code_dataSorce.chargeUserName}}
					</div>
				</div>
			</div>

			<div v-if="deliver_tpye == 1" style="margin-top: 10px;">明细</div>

			<div class="bill_content_mx" v-for="(val,indexs) in Code_dataSorce.orderDetailList">
				<div class="bill_content_list">
					<div class="bill_title">产品编号：</div>
					<div class="bill_input_rst">
						{{val.no}}
					</div>
				</div>
				<div class="bill_content_list">
					<div class="bill_title">颜色：</div>
					<div class="bill_input_rst">
						{{val.colorName}}
					</div>
				</div>
				<div class="bill_content_list">
					<div class="bill_title">数量：</div>
					<div class="bill_input_rst">
						{{val.quantity}}
					</div>
				</div>
			</div>
			<div style="height: 110rpx;"></div>
			<div v-if="deliver_tpye == 0" class="bill_content_button" :style="'width:' + bo_windth" @click="record_up">
				{{type_txt}}
			</div>

		</div>

		<!-- 弹窗组件 -->
		<popus ref="alerts">
			<alertcenter @row="row" @closes="closes" :dataSorc="popup_Data"></alertcenter>
		</popus>

		<!-- 数量弹窗 -->
		<popus ref="alert_sub">
			<div :style="'padding-top:' + nv_height_alert + ';background:#fff;'">
				<div class="alert_out">
					<div class="alert_text_title">请输入数量</div>

					<div class="alert_text" style="color: #555555;">
						<div class="alert_text_left">物料名称：</div>
						<div class="alert_text_right">{{quantity_Nmae}}</div>
					</div>
					<div class="alert_text" style="color: #555555;">
						<div class="alert_text_left">规格型号：</div>
						<div class="alert_text_right">{{quantity_Code}}</div>
					</div>
					<view v-if="unitid_type">
						<div class="alert_text" style="color: #555555;">
							<div class="alert_text_left">辅计量：</div>
								<select-lay :zindex="1" :placeholder="tval_begin" :options="unit_data_list" @selectitem="unitDataList">
							</select-lay>
						</div>	
						<div class="alert_text" style="color: #555555;">
							<div class="alert_text_left">辅计数：</div>
							<div class="alert_text_right" >
								<input v-model="subQuantity" @input="unit_result"/>
							</div>
						</div>
						<div class="alert_text" style="color: #555555;">
							<div class="alert_text_left">换算率：</div>
							<div class="alert_text_right">
								{{unitExchangeRate}}
							</div>
						</div>
					</view>
					<div class="alert_text" style="color: #555555;">
						<div class="alert_text_left">主计量：</div>
						<div class="alert_text_right">{{baseUnitName}}</div>
					</div>
					<div class="alert_text">
						<div class="alert_text_left">数量：</div>
						<div class="alert_text_right"><input v-model="quantity_number" type="number" ref="paw" @input="unit_rst"/></div>
					</div>
					<div class="alert_button" @click='Number_alert(C_Uid)'>
						<button>确定</button>
					</div>
				</div>
			</div>
		</popus>
		
		<!-- 超出最低库存列表 -->
		<popus ref="list_number">
			<view :style="'width:' + bo_windth " >
				<view class="number_list" >
					<view class="number_list_title">
						库存预警
					</view>
					<view class="number_box">
						<view class="number_list_type" v-for="(item,index) in load_list">
							<view class="unmber_flex">
								<view class="number_list_left">
									厂库:
								</view>
								<view class="number_list_right">
									{{item.warehouseName}}
								</view>
							</view>
							<view class="unmber_flex">
								<view class="number_list_left">
									物料:
								</view>
								<view class="number_list_right">
									{{item.inventoryName}}
								</view>
							</view>
							<view class="unmber_flex">
								<view class="number_list_left">
									规格型号:
								</view>
								<view class="number_list_right">
									{{item.inventorySpecification}}
								</view>
							</view>
							<view class="unmber_flex">
								<view class="number_list_left">
									出库数量:
								</view>
								<view class="number_list_right">
									{{item.baseQuantity}}
								</view>
							</view>
							<view class="unmber_flex">
								<view class="number_list_left">
									出库前数量:
								</view>
								<view class="number_list_right">
									{{item.beforeInventoryQuantity}}
								</view>
							</view>
							<view class="unmber_flex">
								<view class="number_list_left">
									出库后数量:
								</view>
								<view class="number_list_right">
									{{item.afterInventoryQuantity}}
								</view>
							</view>
							<view class="unmber_flex">
								<view class="number_list_left">
									最低库存:
								</view>
								<view class="number_list_right" style="color: red;">
									{{item.minStorage}}
								</view>
							</view>
						</view>
					</view>
					<view class="number_list_bottom">
						<view class="number_list_txt" style="background-color: #555555;" @click="numberout">
							取消入库
						</view>
						<view class="number_list_txt" @click="numbercontinue">
							继续入库
						</view>
					</view>
				</view>
			</view>
		</popus>

		<!-- 下拉修改弹窗 -->
		<popus ref="edit_list">
			<div class="conten">
				<div class="conten_edit">
					<div class="conten_edit_type conten_edit_public">
						<div class="conten_edit_left">
							物料编码：
						</div>
						<div class="conten_edit_right">
							{{click_data.inventoryCode}}
						</div>
					</div>
					<div class="conten_edit_type">
						<div class="conten_edit_left">
							物料名称：
						</div>
						<div class="conten_edit_right">
							{{click_data.inventoryName}}
						</div>
					</div>
					<div class="conten_edit_type">
						<div class="conten_edit_left">
							规格型号：
						</div>
						<div class="conten_edit_right">
							{{click_data.inventorySpecification}}
						</div>
					</div>
					<view  v-if="unitid_type">
						<div class="conten_edit_type">
							<div class="conten_edit_left">
								辅计量：
							</div>
							<div class="conten_edit_right">
								<select-lay :zindex="1" :placeholder="tval_begin" :options="unit_data_list" @selectitem="click_uniti">
								</select-lay>
							</div>
						</div>	
						<div class="conten_edit_type">
							<div class="conten_edit_left">
								辅计数：
							</div>
							<div class="conten_edit_right">
								<!-- {{click_data.subQuantity}} -->
								<input v-model="click_data.subQuantity" type="number" @input="unit_subquan"/>
							</div>
						</div>	
						<div class="conten_edit_type">
							<div class="conten_edit_left">
								换算率：
							</div>
							<div class="conten_edit_right">
								{{click_data.unitExchangeRate}}
							</div>
						</div>	
					</view>
					<div class="conten_edit_type ">
						<div class="conten_edit_left">
							数量：
						</div>
						<div class="conten_edit_right ">
							<input v-model="click_data.baseQuantity" type="number" @input="unit_base"/>
						</div>
					</div>
				</div>
				<div class="conten_del" @click="del_list">
					<i class="iconfont icon-shanchu"></i>
				</div>
				<div class="conten_edit_bottom" @click="content_close">
					保&nbsp;&nbsp;存
				</div>
			</div>
		</popus>

		<!-- 选择物料 -->
		<popus ref="bill_alert">
			<div :style="'width:' + bo_windth " class="bill_wiat">
				<billde @bill_end="bill_end"></billde>
			</div>
		</popus>

		<!-- 搜索上拉 -->
		<popus ref="science_al">
			<div :style="'width:' + bo_windth " class="bill_wiat">
				<sciencealer @row="row" @closes="closes" :dataSorc="popup_Data"></sciencealer>
			</div>
		</popus>

		<!-- 扫码 -->
		<moncode :Scan_code="scancode"></moncode>
		
		
	</view>
</template>

<script>
	import navitop from '@/components/common/navi-top'
	import billde from '@/components/common/bill/bill-details-alert.vue'
	import alertcenter from '@/pages/common/common/alert/alertcenter'
	import popus from '@/uni_modules/uni-popup/components/uni-popup/uni-popup'
	import sciencealer from '@/pages/common/common/alert/sciencealert'
	import moncode from "@/components/dumenghanfeng-monitorScanCode/monitorScanCode.vue"
	import {
		mapState
	} from "vuex";
	import {
		current_time
	} from "@/common/api/common/public_api.js"
	import {
		list,
		listhouses,
		pageList,
		pageForVoucher,
		getList,
		inventorypageList,
		recordsave,
		ForVoucher,
		BarCode,
		nssave,
		savecode,
		eliverysave,
		saveAndConfirm
	} from '@/common/api/job/bill.js'
	import {
		UnitPage
	} from '@/common/api/common/tools.js'
	export default {
		components: {
			navitop,
			alertcenter,
			popus,
			moncode,
			billde,
			sciencealer
		},
		data() {
			return {
				titles: '',
				nv_height: '',
				nv_windowWidth: '',
				tval_begin: '请选择辅计量',
				bo_windth: '',
				nv_height_alert: '',
				dataSorce: '', // 获取表字段
				popup_Data: [], // 获取弹窗数据
				data_curl_rst: {
					current: 1,
					size: 10,
					keyWord: '',
					'page.current': 1,
					'page.size': 10

				},
				
				Code_dataSorce: [], // 获取用户扫码结果

				scancode: 'Warehouse', // 扫码自定义事件

				curl_data: '',

				quantity_number: '',

				baseQuantity: '',
				
				baseUnitName: '',	// 主计量
				
				baseUnitId: '',	// 主计量ID
				
				subUnit: '',	// 辅计量
				
				subUnitId: '',	// 辅计量Id
				
				subQuantity: '',	// 辅计量数量
				
				unitExchangeRate: '',	// 换算率

				C_Uid: '', // 区分数量(那个的)

				D_code: '', // 分类

				title_data: [], //扫码数组却从

				deliver_tpye: 0,

				rundata: 0,

				mainId: null, // 发货单主表ID
				detaiMap: new Map(),

				click_data: '',

				type_txt: '',
				text_list: '',
				scan_code: '',
				
				unit_data_list: [],	 
				
				unitid_type: false,	// 是否显示多计量
				
				isCheckInventoryInfo: true,	// 是否检查最低库存
				
				load_list: [],	// 最低库存列表
				
				numberType: false,	// 防止重复点击提交数据错乱
				
				thisData: {}, // 重复点击历史记录
				
			}
		},
		onLoad(option) {
			this.D_code = option.code;
			this.curl_data = option;
			this.titles = option.title;
			if (option.code == "NSSaleDispatchVoucher") {
				this.deliver_tpye = 1;
			}

			if (option.c_code == 1) {
				this.text_list = '实收数量'
			} else {
				this.text_list = '实发数量'
			}
			if (option.c_code == 1) {
				if (this.curl_data.code == 'SaleDelivery') {
					this.type_txt = '提交审核'
				} else {
					this.type_txt = '提交入库'
				}
			} else if (option.c_code == 2) {
				if (this.curl_data.code == 'ProductReceiveVoucher') {
					this.type_txt = '提交出库'
				} else {
					this.type_txt = '提交出库'
				}
			} else if (option.c_code == 0) {
				this.type_txt = '提交审核'
			}
			let _this = this;
			uni.$off(this.scancode);
			uni.$on(this.scancode, function(rst) {
				console.log('扫码结果', rst.msg)
				_this.scan_code = rst.msg; // 获取扫码结果
				_this.inventory_list(rst.msg);
			})
		},
		computed: {
			...mapState({
				unitData: state => state.unitData,
			})
		},
		mounted() {
			this.nv_height = uni.getSystemInfoSync().statusBarHeight + 60 + 'px';

			this.nv_height_alert = uni.getSystemInfoSync().statusBarHeight + 15 + 'px';

			this.nv_windowWidth = uni.getSystemInfoSync().windowWidth - 20 + 'px';

			this.bo_windth = uni.getSystemInfoSync().windowWidth + 'px';

			this.bill_list();

			this.$store.dispatch('getUnitSubData');
			
		},
		methods: {

			// 返回 
			outlot() {
				uni.navigateTo({
					url: '/pages/common/index/purchase/common?title=' + this.curl_data.title + '&code=' + this
						.curl_data.code + '&sort=' + this.curl_data.sort + '&c_code=' + this.curl_data.c_code +
						'&key=' + this
						.curl_data.key + '&search_code=' + this.curl_data.search_code
				})
			},

			// 弹窗保存
			content_close() {
				this.$refs.edit_list.close();
			},

			//  点击获取数据
			content_click(val) {
				console.log(val)
				this.click_data = val;
				if(val.unitGroupId){
					let rundata = [];
					let _this = this;
					_this.subUnit = _this.unit_data_list[0];
					_this.subUnitId = _this.unit_data_list[0].value.id;
					this.click_data['unitExchangeRate'] = this.unit_data_list[0].value.changeRate;
				}
				if(val.unitGroupId){
					this.unitid_type = true;
				}else {
					this.unitid_type = false;
				}
				this.$refs.edit_list.open('bottom')
			},
			
			// 上拉选择辅计量并计算
			click_uniti(val){
				// 辅计量切换单位
				this.unitExchangeRate = this.unit_data_list[val].value.changeRate;
				this.tval_begin = this.unit_data_list[val].value.name;
				this.quantity_number = this.unitExchangeRate * this.subQuantity;
				// 换算率
				this.click_data['unitExchangeRate'] = this.unit_data_list[val].value.changeRate;
				// 辅计量名称
				this.click_data['subUnitName'] = this.unit_data_list[val].value.name;
				// 数量
				this.click_data['baseQuantity'] = this.click_data['unitExchangeRate'] * this.click_data['subQuantity'];
				
			},

			// 上拉输入辅计数
			unit_subquan(val){
				
				this.click_data['baseQuantity'] = this.click_data['subQuantity'] * this.click_data['unitExchangeRate'];
				
			},
			
			// 上拉输入数量
			unit_base(val){
				
				this.click_data['subQuantity'] = this.click_data['baseQuantity'] / this.click_data['unitExchangeRate'];
				
			},

			// 点击删除
			del_list() {
				for (let i = 0; i < this.Code_dataSorce.length; i++) {
					if (this.click_data.id == this.Code_dataSorce[i].id) {
						this.Code_dataSorce.splice(i, 1);
					}
				}
				this.$refs.edit_list.close();
			},

			// 切换辅计量
			unitDataList(val){
				this.unitExchangeRate = this.unit_data_list[val].value.changeRate;
				this.tval_begin = this.unit_data_list[val].value.name;
				this.quantity_number = this.unitExchangeRate * this.subQuantity;
			},
			
			//辅计数
			unit_result(type){
				
				this.quantity_number = this.unitExchangeRate * type.target.value;
				
			},
			
			//主计量数量
			unit_rst(type){
				this.subQuantity = type.target.value / this.unitExchangeRate;
				
			},
			

			// 获取选中物料 
			bill_end(val) {
				console.log(val)
				if(val){
					this.unitid_type = false;
					let _this = this;
					let rundata = [];
					let unit_list = [];
					let repeatData = '';
					this.unit_data_list = [];
					if (val) {
						repeatData = this.Code_dataSorce.filter(item => item.id == val.id)
						if(repeatData.length == 0){
							this.C_Uid = val.id;
							this.Code_dataSorce.push(val)
						}
						
						if(val.unitType == "多计量") {
							this.quantity_Nmae = val.inventoryName;
							this.baseUnitName = val.mainUnitName;
							this.baseUnitId = val.mainUnit;
						}else{
							this.quantity_Nmae = val.inventoryName;
							this.baseUnitName = val.unitName;
							this.baseUnitId = val.unitId;
						}
						
						this.quantity_Code = val.inventorySpecification;
						this.$refs.alert_sub.open('top');
					}
					
					unit_list = this.unitData.get(val.unitGroupId);
					
					console.log(unit_list)
					
					if(unit_list) {
						
						unit_list.forEach(item => {
							_this.unit_data_list.push( {label: item.name,value: item})
						})
						_this.subUnit = _this.unit_data_list[0];
						_this.subUnitId = unit_list[0].id;
						this.tval_begin = unit_list[0].name;
						_this.unitExchangeRate = unit_list[0].changeRate;
					}
					this.$nextTick(() => {
						if(val.unitGroupId){
							this.unitid_type = true;
						}else {
							this.unitid_type = false;
						}
					});
					this.$refs.bill_alert.close();
				}else{
					this.$refs.bill_alert.close();
				}
				
			},

			// 点击获取物料信息
			material() {
				this.$refs.bill_alert.open('right')
			},

			// 点击获取扫码(相机)
			sweep_code() {
				// uni.removeStorageSync('details_dataSorce');
				let _this = this;
				uni.scanCode({
					onlyFromCamera: true,
					success: function(res) {
						console.log('条码类型：' + res.scanType);
						console.log('条码内容：' + res.result);
						_this.scan_code = res.result; // 获取扫码结果
						_this.inventory_list(res.result);
					}
				});
			},

			// 获取发货单数据
			deliver_list(data) {
				const {
					mainId,
					detaiMap
				} = this;

				let _this = this;
				let data_run = {};

				if (mainId) {
					data_run = {
						barcode: data,
						mainId
					};
				} else {
					data_run = {
						barcode: data
					};
				}


				// 发货添加
				savecode(data_run).then((rows) => {
					if (rows.code == 200) {
						this.rundata = 1;
						let detail = rows.data.orderDetailList[0];
						let sourceDetailId = rows.data.orderDetailList[0].sourceDetailId;

						if (mainId) {
							let oldDetail = detaiMap.get(sourceDetailId);
							if (oldDetail) {
								console.log('找到明细')
								oldDetail.quantity = detail.quantity;
							} else {
								detaiMap.set(sourceDetailId, detail);

								this.Code_dataSorce.orderDetailList.push(detail);
							}
						} else {
							this.Code_dataSorce = rows.data;
							detaiMap.set(sourceDetailId, detail);
							this.mainId = rows.data.id;
						}
					} else {
						console.log(rows)
					}
				}).catch((e) => {
					uni.showToast({
						title: '提示'
					})
				})

			},

			// 扫码获取物料
			inventory_list(val) {
				let _this = this;
				let copy_type = '';
				let code_data = [];
				let rundata = [];
				let unit_list = '';
				this.C_Uid = '';
				this.unit_data_list = [];
				copy_type = this.Code_dataSorce.filter(item => item.inventoryCode == this.scan_code);
				if (copy_type.length == 0) {
					inventorypageList({code: val }).then((rows) => {
						if (rows.code == 200) {
							console.log(rows)
							this.C_Uid = rows.data.id;
							this.quantity_Nmae = rows.data.inventoryName;
							this.quantity_Code = rows.data.inventorySpecification;
							this.baseUnitName = rows.data.mainUnitName;
							this.baseUnitId = rows.data.mainUnit;
							this.Code_dataSorce.push(rows.data);
							
							unit_list = this.unitData.get(rows.data.unitGroupId);
							if(unit_list) {
								unit_list.forEach(item => {
									_this.unit_data_list.push( {label: item.name,value: item})
								})
								_this.subUnit = _this.unit_data_list[0];
								_this.subUnitId = unit_list[0].id;
								this.tval_begin = unit_list[0].name;
								_this.unitExchangeRate = unit_list[0].changeRate;
							}
							 
							if (code_data) {
								code_data.filter(item => {
									_this.Code_dataSorce.push(item)
								})
							}
							
								if(rows.data.unitGroupId){
									this.unitid_type = true;
								}else {
									this.unitid_type = false;
								}
						}
					})
				} else {
					uni.showModal({
						title: '提示',
						content: '扫码重复，是否修改数量',
						success: function(res) {
							if (res.confirm == true) {
								_this.C_Uid = copy_type[0].id;
								_this.quantity_Nmae = copy_type[0].inventoryName;
								_this.quantity_Code = copy_type[0].inventorySpecification;
							}
						}
					})
				}
				
			
				
				
			
				_this.$refs.alert_sub.open('top');
			},

			//	点击获取输入数量  
			Number_alert(val) {
				this.baseQuantity = this.quantity_number;
				let _this = this;	
				this.Code_dataSorce.forEach((item) => {
					if (item.id == _this.C_Uid) {
						this.$set(item, 'baseUnit', this.baseUnitId);	// 主计量id
						this.$set(item, 'baseUnitName', this.baseUnitName);// 主计量名称
						this.$set(item, 'baseQuantity', this.quantity_number);	// 数量
						this.$set(item, 'unitExchangeRate', this.unitExchangeRate);// 换算率
						this.$set(item, 'subUnit', this.subUnitId);	// 辅计量
						this.$set(item, 'subUnitName', this.tval_begin);// 辅计量名称
						this.$set(item, 'subQuantity', this.subQuantity);// 辅计量数量
						
					}
				})
				
				
				this.$refs.alert_sub.close();
				this.quantity_number = '';
			},

			// 修改数量
			Nub_alert(val) {
				this.C_Uid = val.id;
				this.$refs.alert_sub.open('top');
			},


			// 获取列表字段
			bill_list() {
				let _this = this;
				let pot = {};
				list({
					source: this.D_code
				}).then((rows) => {
					if (rows.code == 200) {
						this.dataSorce = rows.data;
					}
				})
			},

			// 弹窗处理(数据处理)
			choice(val) {
				this.popup_Data = [];
				if (val.code == "warehouseId") {
					this.warehouse_list(val.code);
				}
				if (val.code == 'purchaseTypeId') {
					this.purchase_list(val.code);
				}
				if (val.code == 'vendorId') {
					this.voucher_list(val.code);
				}
				if (val.code == 'businessType') {
					let run = JSON.parse(val.val);
					run.forEach((item) => {
						this.$set(item, 'name', item.label);
						this.$set(item, 'id', item.key);
						this.$set(item, 'code', 'businessType');
					})
					this.popup_Data = run;
				}
				if (val.code == 'crmId') {
					this.User_list('crmId');
				}

				this.$refs.science_al.open('bottom');
			},

			// 弹窗处理(获取弹窗结果)
			row(val) {
				let pot = {};
				if (val.code !== 'businessType') {
					pot['value'] = val.name;
					pot['code'] = val.code.split('Id')[0] + 'Name';
					this.dataSorce.filter((item) => {
						if (item.code == val.code) {
							if (val.code.substr(val.code.length - 2, 2) == 'Id') {
								this.$set(item, 'values', val.name)
								this.$set(item, 'value', val.id)
							} else {
								this.$set(item, 'value', val.name)
							}
						}
					})
					this.dataSorce.push(pot);
				} else {
					this.dataSorce.filter((item) => {
						if (item.code == val.code) {
							this.$set(item, 'values', val.name);
							this.$set(item, 'value', val.id);
						}
					})
				}
				this.$refs.science_al.close();
			},

			// 获取客户
			User_list(valCode) {
				let _this = this;
				ForVoucher({
					current: 1,
					size: -1,
					isFinance: 1,
					companyType: 'isCustomer',
					'page.current': 1,
					'page.size': -1
				}).then((rst) => {
					if (rst.code == 200) {
						rst.data.records.filter((item) => {
							_this.popup_Data.push({
								name: item.crmName,
								code: valCode,
								id: item.id
							})
						})
					}
				})
			},

			// 获取仓库
			warehouse_list(valCode) {
				let _this = this;
				listhouses(this.data_curl_rst).then((rst) => {
					if (rst.code == 200) {
						rst.data.records.filter((item) => {
							_this.popup_Data.push({
								name: item.whName,
								code: valCode,
								id: item.id,
								whCode: item.whCode
							})
						})
					}
				})
			},

			// 获取采购列表
			purchase_list(valCode) {
				let _this = this;
				pageList(this.data_curl_rst).then((rows) => {
					if (rows.code == 200) {
						rows.data.records.filter((item) => {
							_this.popup_Data.push({
								name: item.name,
								code: valCode,
								id: item.id,
								D_Code: item.code
							})
						})
					}
				})
			},

			// 获取供应商

			voucher_list(valCode) {
				let _this = this;
				let coucherdata = {
					current: 1,
					size: -1,
					isFinance: 1,
					companyType: 'isSupplier',
					'page.current': 1,
					'page.size': -1
				};
				pageForVoucher(coucherdata).then((rows) => {
					if (rows.code == 200) {
						rows.data.records.filter((item) => {
							_this.popup_Data.push({
								name: item.crmName,
								code: valCode,
								id: item.id,
								D_Code: item.crmNo
							})
						})
					}
				})
			},
		
			// 提交
			record_up() {
				let dats = {};
			
				if(!this.numberType){
						
					this.numberType = true;
					this.dataSorce.filter((item) => {
						dats[item.code] = item.value;
					})
				
					let wherehouse = this.dataSorce.find(res => {
						return res.code == 'warehouseId'
					}); //仓库

					let vendorId = this.dataSorce.find(res => {
						return res.code == 'vendorId'
					}); //供应商

					if (wherehouse && wherehouse.value == null) {
						uni.showToast({
							title: '仓库必须填写',
							icon: "error"
						})
						return;
					}
					if (vendorId && vendorId.value == null) {
						uni.showToast({
							title: '供应商必须填写',
							icon: "error"
						})
						return;
					}
				console.log('---------',this.Code_dataSorce[0])
					this.Code_dataSorce.forEach((item) => {
						console.log('---------',item.id)
						this.$set(item, 'inventoryId', item.id);
						if (item.unitType == "单计量") {
							this.$set(item, 'baseUnit', item.unitId)
							this.$set(item, 'unitId', item.unitId)
							this.$set(item, 'baseUnitName', item.unitName)
							this.$set(item, 'subUnit', null)
							this.$set(item, 'unitGroupId', null)
							this.$set(item, 'subUnitName', null)
							this.$set(item, 'subQuantity', null)
						} else {
							this.$set(item, 'baseUnit', item.mainUnit)
							this.$set(item, 'unitGroupId', item.unitGroupId)
							this.$set(item, 'baseUnitName', item.mainUnitName)
						}

						delete item.id;
						delete item.warehouseId;
						delete item.warehouseName;
					})
					dats['receiveDeliverType'] = this.curl_data.c_code;
					dats['orderDetailList'] = this.Code_dataSorce;
					dats['dataSource'] = this.D_code;
					dats['voucherDate'] = current_time();
					if (this.curl_data.c_code == 0 && this.curl_data.code == 'SaleDispatchVoucher') {
						dats['businessType'] = 98;
						// dats['receiveDeliverType'] = 2;
					} 
					if (this.curl_data.code == "OtherReceiveVoucher") {
						//其他入库单
						dats['inDate'] = current_time();
						dats['businessType'] = 109;

					}
					if (this.curl_data.code == "OtherDispatchVoucher") {
						//其他出库单
						dats['inDate'] = current_time();
						dats['businessType'] = 110;
					}
					if (this.curl_data.c_code == 2) {
						//采购退货单
						dats['deliveryDate'] = current_time();
						if (this.curl_data.code == 'PurchaseReceiveVoucher') {
							dats['voucherState'] = 2;
							dats['businessType'] = 99;
						}
					}
					if (this.curl_data.c_code == 3) {
						dats['businessType'] = 99;
						// dats['receiveDeliverType'] = 2;
						console.log(1)
					}
					if (this.curl_data.code == 'DelegateReceiveVoucher' && this.curl_data.c_code == 2) {
						// 委外产品退库
						dats['businessType'] = 95;
					}
					if (this.curl_data.code == "ProductReceiveVoucher" && this.curl_data.c_code == 2) {
						// 产品退库
						dats['businessType'] = 94;
					}
					if (this.curl_data.code == "SaleDispatchVoucher" && this.curl_data.c_code == 1) {
						//销售退货单
						dats['businessType'] = 98;
					}
					
					this.thisData = dats;
					// console.log(dats)
				}else{
					dats = this.thisData;
				} 
				if (this.curl_data.code == "SaleDelivery") {
					if (this.curl_data.c_code == 1) {
						dats['businessType'] = 98;
					} else if (this.curl_data.c_code == 0) {
						dats['businessType'] = 0;
					}
					eliverysave(dats).then(item => { // 退货通知单
						if (item.code == 200) {
							this.url_Jump();
						} else {
							uni.showToast({
								title: rows.message,
								icon: "error"
							})
						}
					})
				} else
				// if(this.curl_data.code == "ProductReceiveVoucher" && this.curl_data.c_code == 2 ) {
				// 	dats['businessType'] = 94;
				// 	this.prese_data(dats);
				// }else 
				// if(this.curl_data.code == "SaleDispatchVoucher" && this.curl_data.c_code == 1) {
				// 	dats['businessType'] = 98;
				// 	this.prese_data(dats);
				// }else 
				{
					console.log('直接入库')
					// 是否校验最低库存
					dats['isCheckInventoryInfo'] = this.isCheckInventoryInfo;
					saveAndConfirm(dats).then((rows) => {
						if (rows.code == 200) {
							this.url_Jump();
						} else if(rows.code == 5002) {
							// console.log("保存-----------------",JSON.parse(rows.message));
							this.isCheckInventoryInfo = false;
							this.load_list = JSON.parse(rows.message);
							this.$refs.list_number.open('center');
							
						}else{
							uni.showToast({
								title: rows.message,
								icon: "none"
							})
						}
					})
				}
			},

			// 保存待提交
			prese_data(val) {
				recordsave(val).then(item => {
					if (item.code == 200) {
						this.url_Jump();
					} else {
						uni.showToast({
							title: item.message,
							icon: "none"
						})
					}
				})
			},

			// 路由跳转
			url_Jump() {
				let tua = '';
				if (this.curl_data.c_code == 1) {
					if (this.curl_data.code == "PurchaseReceiveVoucher" ||
						this.curl_data.code == "SaleDelivery" ||
						this.curl_data.code == "RRS" ||
						this.curl_data.code == "MaterialDispatchVoucher" ||
						this.curl_data.code == "SaleDispatchVoucher" ||
						this.curl_data.code == "ProductReceiveVoucher" ||
						this.curl_data.code == "DelegateRRS" ||
						this.curl_data.code == "DelegateDispatchVoucher" ||
						this.curl_data.code == "DelegateReceiveVoucher" ||
						this.curl_data.code == "OtherReceiveVoucher"
					) {
						tua = '提交并入库！'
					}
				}
				if (this.curl_data.c_code == 2) {
					if (this.curl_data.code == "PurchaseReceiveVoucher" ||
						// this.curl_data.code == "SaleDispatchVoucher" ||
						this.curl_data.code == "SaleDelivery" ||
						this.curl_data.code == "ProductReceiveVoucher" ||
						this.curl_data.code == "MaterialDispatchVoucher" ||
						this.curl_data.code == "DelegateDispatchVoucher" ||
						this.curl_data.code == "DelegateReceiveVoucher" ||
						this.curl_data.code == "OtherDispatchVoucher"
					) {
						tua = '提交并出库！'
					}
				}

				if (this.curl_data.c_code == 0 || this.curl_data.c_code == 1 || this.curl_data.c_code == 2) {
					if (this.curl_data.c_code == 1 && this.curl_data.code == 'ProductReceiveVoucher') {
						tua = '提交并入库！'
					} else if (this.curl_data.c_code == 2 && this.curl_data.code == 'SaleDispatchVoucher') {
						tua = '提交并出库！'
					} else {
						if (this.curl_data.code == 'SaleDelivery') {
							tua = '提交待审核！'
						}
					}
				}


				uni.showToast({
					title: tua,
					icon: 'none'
				})
				setTimeout(() => {
					// uni.navigateBack()
					uni.navigateTo({
						url: '/pages/common/index/purchase/common?title=' + this.curl_data.title +
							'&code=' + this.curl_data.code + '&sort=' + this.curl_data.sort + '&c_code=' +
							this.curl_data.c_code + '&key=' + this.curl_data.key + '&search_code=' + this
							.curl_data.search_code
					})
				}, 1500)
			},
			
			// 取消
			numberout(){
				 this.isCheckInventoryInfo = true;
				this.$refs.list_number.close();
			},
			
			// 确定入库
			numbercontinue(){
				this.record_up();
			}
			
		}
	}
</script>

<style scoped lang="scss">
	
	.number_list {
		width: 95%;
		margin: auto;
		background-color: #fff;
		border-radius: 4px;
		height: 500px;
		
		.number_list_title {
			text-align: center;
			padding: 5px 0;
		}
		.number_box {
			height: 410px;
			overflow-y: scroll;
		}
		
		.number_list_type {
			padding: 5px 0;
			border-bottom: 1px solid #cacaca;
			.unmber_flex {
				display: flex;
			}
			.number_list_left {
				width: 100px;
				text-align: right;
				padding: 0 5px 0 0;
				overflow: hidden;
				white-space: nowrap;
			}
			.number_list_right {
				overflow: hidden;
			}
		}
		
		.number_list_bottom {
			display: flex;
			justify-content: space-around;
		}
		.number_list_txt {
			background-color: #007AFF;
			color: #fff;
			text-align: center;
			padding: 5px 10px;
			border-radius: 4px;
			box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.2);
		}
	}
	
	.alert_text_title {
		padding: 10px 0 10px 30rpx;
		font-size: 20px;
		border-bottom: 1px solid #cacaca;
	}

	.bill_wiat {
		width: 100%;
		height: 100%;
		background-color: #fff;
	}


	.remaks_data {
		height: 90rpx;
		line-height: 90rpx;
		text-align: center;
		color: #cacaca;
	}

	// 产品选择样式
	.bill_manual {
		background-color: #fff;
		text-align: center;
		width: 100%;
		margin: auto;
	}

	.bill_manual_choose {
		border-top: 1px solid #cacaca;
		padding: 10px 0 0 0;
		width: 95%;
		margin: auto;
		height: 90rpx;
		line-height: 90rpx;

		span {
			padding: 1% 8% 1% 8%;
			border-radius: 4px;
			background-color: #007AFF;
			color: #fff;
			margin: 10px;
		}
	}

	// 备注模块

	.remarks_inpt {
		width: 95%;
		margin: auto;
		padding: 0 0 90rpx 0;

		textarea {
			width: 100%;
			height: 120rpx;
			margin: 10px 0 10px 0;
		}
	}

	.remarks_txt {
		width: 95%;
		margin: auto;
		height: 60rpx;
		line-height: 60rpx;
		border-bottom: 1px solid #cacaca;
	}

	.remarks_style {
		margin: 10px 0 0 0;
		background-color: #fff;
		box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
	}


	.bill_content_button {
		height: 90rpx;
		line-height: 90rpx;
		background-color: rgb(34, 131, 228);
		text-align: center;
		color: #fff;
		border-radius: 4px;
		box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
		margin: 10px 0 0 0;
		position: fixed;
		bottom: 0;
	}

	.alert_button {
		padding: 0 0 5px 0;

		button {
			width: 60%;
			background-color: rgb(34, 131, 228);
			color: #fff;
		}
	}

	.alert_out {
		background-color: #fff;
		border-radius: 4px;
		box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
	}

	.alert_text {
		display: flex;
		width: 100%;
		height: 90rpx;
		line-height: 90rpx;
		// margin: 10px 0 10px 0;
		// padding: 10px 0 10px 0;
	}

	.alert_text_left {
		width: 30%;
		text-align: right;
	}

	.bill_sty {
		background-color: #fff;
		padding: 10px 0 10px 0;
	}

	.bill_list {
		width: 90%;
		margin: auto;
		height: 70rpx;
		line-height: 70rpx;
		padding: 0 0 0 30rpx;
		display: flex;
	}

	.bill_title {
		width: 30%;
		overflow: hidden;
		text-align: left;
	}

	.alert_text_right {
		width: 68%;

		input {
			border: 1px solid #cacaca;
			width: 90%;
		}
	}

	.bill_input {
		width: 65%;

		input {
			width: 90%;
			border: 1px solid #cacaca;
			height: 50rpx;
		}
	}

	.bill_input_rst {
		width: 65%;
		margin: 0 0 0 20rpx;
	}

	// 数据列表
	.bill_content {
		background-color: #fff;
		margin-top: 10px;
		padding: 10rpx 0 10px 0;
	}

	.bill_content_mx {
		background-color: #fff;
	}

	.bill_content_list {
		width: 90%;
		margin: auto;
		display: flex;
		height: 60rpx;
		line-height: 60rpx;
	}

	.bill_content_title {
		padding: 10rpx 0 10rpx 30rpx;
		margin-bottom: 5px;
	}


	// 上拉修改弹窗

	.conten_del {
		position: fixed;
		top: 0;
		right: 5%;
		color: red;
		padding: 10px 10px 10px 10px;
		font-size: 20px;
	}

	.conten_edit_input {
		width: 50%;
	}

	.conten_edit_inputs {
		display: flex;
		justify-content: space-between;

		input {
			border: 0;
		}
	}

	.conten_edit_bottom {
		height: 50px;
		line-height: 50px;
		text-align: center;
		background-color: #1890FF;
		color: #fff;
		font-size: 18px;
		border-radius: 4px;
		box-shadow: 0 12px 2px 0 rgba(0, 0, 0, 0.1);
	}

	.conten_edit_public {
		padding-top: 5px;
	}

	.conten_edit_bod {
		border-top: 0 !important;
		border-bottom: 0 !important;
	}

	.conten_edit_right_bod {
		border-right: 0 !important;
	}

	.conten_edit_plus {
		display: flex;
		text-align: center;

		div {
			border: 1px dotted #cacaca;
			width: 80rpx;
		}
	}

	.conten_edit_type {
		display: flex;
		border-bottom: 1px dotted #cacaca;
		// overflow: hidden;
	}

	.conten_edit_left {
		width: 30%;
	}

	.conten_edit_right {
		width: 70%;
	}

	.conten_edit {
		height: 310px;
		width: 90%;
		margin: auto;

		div {
			height: 80rpx;
			line-height: 80rpx;
		}
	}

	.conten {
		width: 100%;
		background-color: #fff;
	}
</style>
